/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JList;
import javax.swing.ListModel;

/**
 * 
 * @author TienNT
 */
public class Client_Dashboard extends javax.swing.JFrame implements Runnable {
	Client mClient;
	ContactEntry[] mContactEntries;

	/**
	 * Creates new form Server
	 */
	public Client_Dashboard(Client client) {
		mClient = client;
		System.out.println("Client: " + mClient.getClientName());
		initComponents();
		updateContactList();
	}

	public void initContactEntries() {
		Scanner scanner = null;
		try {
			FileInputStream fileInputStream = new FileInputStream(new File(
					"F:/temp"));
			scanner = new Scanner(fileInputStream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (scanner != null) {
			int size = Integer.parseInt(scanner.nextLine());
			mContactEntries = new ContactEntry[size - 1];
			int count = 0;
			while (scanner.hasNext()) {
				String name = scanner.nextLine();
				if (!name.equalsIgnoreCase(mClient.getClientName())) {
					mContactEntries[count++] = new ContactEntry(name,
							"F:/icon.png");
				}
			}
		}
	}

	public void updateContactList() {
		initContactEntries();
		listContacts.setListData(mContactEntries);
		listContacts.setCellRenderer(new ContactCellRenderer());
		jScrollPane1.setViewportView(listContacts);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		listContacts = new javax.swing.JList();
		jMenuBar1 = new javax.swing.JMenuBar();
		jMenu1 = new javax.swing.JMenu();
		jMenu2 = new javax.swing.JMenu();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle(mClient.getClientName());

		listContacts.addMouseListener(mouseAdapter);
		jScrollPane1.setViewportView(listContacts);

		jMenu1.setText("Messenger");
		jMenuBar1.add(jMenu1);

		jMenu2.setText("Help");
		jMenuBar1.add(jMenu2);

		setJMenuBar(jMenuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 292,
								Short.MAX_VALUE).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup()
						.addContainerGap(76, Short.MAX_VALUE)
						.addComponent(jScrollPane1,
								javax.swing.GroupLayout.PREFERRED_SIZE, 405,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	MouseAdapter mouseAdapter = new MouseAdapter() {
		public void mouseClicked(MouseEvent evt) {
			JList list = (JList) evt.getSource();
			if (evt.getClickCount() == 2) {

				int index = list.locationToIndex(evt.getPoint());
				ListModel dlm = list.getModel();
				Object item = dlm.getElementAt(index);
				;
				list.ensureIndexIsVisible(index);
				System.out.println("Double clicked on " + index + " : " + item);

			}
		}

	};

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JMenu jMenu1;
	private javax.swing.JMenu jMenu2;
	private javax.swing.JMenuBar jMenuBar1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JList listContacts;

	// End of variables declaration//GEN-END:variables

	@Override
	public void run() {
		DataInputStream in = mClient.getInputStream();
		// TODO Auto-generated method stub
		while (true) {
			try {
				Thread.sleep(100);
				int command = in.readInt();
				if (command == ClientUtils.CONTACT_LIST_CHANGED) {
					updateContactList();
				}
			} catch (InterruptedException ex) {

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
